<%-- 
    Document   : orderDetails
    Created on : Jul 9, 2011, 3:56:28 PM
    Author     : ThanhToan
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="dao.OrderDAO" %>
<%@ page import="dao.OrderDetailDAO" %>
<%@ page import="model.OrderDetailsModel" %>
<%@ page import="common.Constant" %>
<%@ page import="java.util.ArrayList"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Order Details Page</title>

   <c:choose>
        <c:when test = "${empty param.orderID}">
                <c:set var="shopCard" value="${requestScope.shoppingCard}" scope="session" ></c:set>
        </c:when>
        <c:otherwise>
                <c:set var="shopCard" value = '<%= OrderDetailDAO.getOrderDetailsByOrderID(request.getParameter("orderID")) %>' scope="session"></c:set>
        </c:otherwise>
    </c:choose>

    <c:set var="status" value = '<%= request.getParameter("status") %>' scope="session"></c:set>

    <script type="text/javascript" language="JavaScript">
        function changeNum(){
            var i = 1;
            var totalAmount = 0;
            var row = document.getElementById('txtNo').value;
            var discount = document.getElementById('discount').value;
            for(i; i <= row; i++){
                var ckNum = document.getElementById('txtNum'+i).value;
                if(ckNum != ''){
                    if(isNaN(ckNum)){
                        document.getElementById('txtNum' + i).value = "1";
                        alert("Number must be a integers!");
                        return;
                    }
                    ckNum = ckNum.replace("-","");
                    if((parseInt(ckNum) - 0) + "" != document.getElementById('txtNum' + i).value){
                        document.getElementById('txtNum' + i).value = "1";
                        alert("Number must be positive integers!");
                        return;
                    }
                    if(document.getElementById('txtNum' + i).value == ""){
                        document.getElementById('txtNum' + i).value = "1";
                    }
                    document.getElementById('txtAmount' + i).value = document.getElementById('txtNum' + i).value * document.getElementById('txtPrice' + i).value;
                }
                totalAmount+=parseInt(document.getElementById('txtAmount' + i).value);
            }
            document.getElementById('totalAmount1').value = totalAmount;
            document.getElementById('totalAmount').value = totalAmount - ((totalAmount*discount)/100);
        }

        function submitForm(){
            document.forms["showCart"].submit();
        }

        function checkoutForm(){
            document.getElementById("action").value = "update";
            submitForm();
        }

        function confirmDeleteRow() {
            document.getElementById("action").value = 'remove';
            <%
                response.setContentType("text/html;charset=UTF-8");
                out.print("var msg='';\n");

                for(Integer i = 1; i <= Constant.NUM_ROWS ; i++) {
                    out.write("if(document.showCart.chkRow" + i.toString() +".checked == true){\n");
                    out.write("msg += ' " + i.toString() + " - '; } \n");
                }
                out.write("if(msg == '') {\n");
                out.write("alert('Chose delete row!');\n");
                out.write("} else {\n");
                out.write("if (confirm('Are you sure to delete ' + msg + '?')) \n submitForm(); \n");
                out.write("}\n");
            %>
	}

        function checkAll(){
            <%
                for(Integer i = 1; i <= Constant.NUM_ROWS ; i++) {
                    out.write("if(document.showCart.chkRow" + i.toString() +".checked == false){\n");
                    out.write("document.showCart.chkRow" + i.toString() +".checked = true}\n");
                    out.write("else{document.showCart.chkRow" + i.toString() +".checked = false}\n");
                }
            %>
        }

        function confirmReview(x)
	{
            var act = '';
            document.getElementById('review').value = 'review';
            if(x == 'A')
            {
                    act = 'approve';
                    document.getElementById('action1').value = act;
            }
            else
            {
                    var value = false;
                    value = window.showModalDialog("inputReason.jsp","Arg1","dialogHeight: 200px; dialogWidth: 300px; dialogTop: 250px; dialogLeft: 350px; edge: Raised; center: Yes; help: No; scroll: No; status: Yes;");
                    if(value != false && value != null)
                    {
                            act = 'reject';
                            document.getElementById('action1').value = act;
                            document.getElementById('reason').value = value;
                    }
                    else
                            return;
            }
            if(confirm("Are you sure to " + act + " this order?"))
                document.forms['checkReview'].submit();
	}

        function changeDiscount(){
            document.getElementById('discount').value = document.getElementById('txtDiscount').value;
        }
    </script>

    </head>
    <body onload="changeNum();changeDiscount();">
        <div id="main_container">
		<!-- S HEAD CONTENT -->
			<jsp:include page="block/header.jsp" />
		<!-- E HEAD CONTENT -->

		<!-- S Top Menu -->
			<jsp:include page="block/top_menu.jsp" />
		<!-- E Top Menu -->

                <!-- S Navigation -->
			<jsp:include page="block/navigation.jsp" />
		<!-- E Navigation -->

                <!-- S Left Menu -->
			<jsp:include page="block/left_menu.jsp" />
		<!-- E Left Menu -->
                <c:set var="integer" value="1"></c:set>

                <div class="center_content">
                    
                <form id="showCart" name="showCart" action="OrderDetailController" method="post" style="background: transparent">
                    <table cellpadding="0" cellspacing="0" border="0" style="background: transparent" align="center">
                        <tr>
                            <th colspan="5" align="center"><h1>Order Details</h1><br/></th>
                        </tr>
                    </table>
                    <table cellpadding="0" cellspacing="0" border="2" style="background: transparent" align="center">
                        <c:if test="${not empty shopCard}">
                            <tr>
                                <th bgcolor= '#186fb2' valign="middle"><div class = "div_txtintable1">
                                    <input type="checkbox" id="chkAll" name="chkAll" onclick="checkAll()"
                                        <c:if test="${status eq 1 or status eq 3 or sessionScope.role eq 'Dealer'}">
                                            disabled
                                        </c:if>
                                       />
                                </div></th>
                                <th bgcolor= '#186fb2' width="30px" valign="middle"><div class = "div_txtintable1">No.</div></th>
                                <th bgcolor= '#186fb2' valign="middle"><div class = "div_txtintable1">Product Name</div></th>
                                <th bgcolor= '#186fb2' valign="middle"><div class = "div_txtintable1">Price</div></th>
                                <th bgcolor= '#186fb2' valign="middle"><div class = "div_txtintable1">Number</div></th>
                                <th bgcolor= '#186fb2' valign="middle"><div class = "div_txtintable1">Amount($)</div></th>
                            </tr>
                        <c:forEach var="objShopCard" items="${shopCard}">
                            <tr <c:if test="${integer % 2 eq 0}"> style="background-color: silver" </c:if>>
                                <td>
                                    <input type="checkbox" name="chkRow${integer}"
                                        <c:if test="${status eq 1 or status eq 3 or sessionScope.role eq 'Dealer'}">
                                            disabled
                                        </c:if>
                                    />
                                </td>
                                <td align="center">${integer}</td>
                                <td>
                                    <input type="text" id="txtProductName${integer}" name="txtProductName${integer}" value="${objShopCard.productName}" readonly style="background-color: transparent"/>
                                    <input type="hidden" id="txtProductID${integer}" name="txtProductID${integer}" value="${objShopCard.productID}" />
                                    <input type="hidden" id="txtID${integer}" name="txtID${integer}" value="${objShopCard.id}" />
                                    <input type="hidden" id="txtDiscount" name="txtDiscount" value="${objShopCard.discount}" onchange="changeDiscount()" />
                                </td>
                                <td><input type="text" name="txtPrice${integer}" id="txtPrice${integer}" value="${objShopCard.price}" readonly style="background-color: transparent"/></td>
                                <td><input type="text" name="txtNum${integer}" id="txtNum${integer}" value="${objShopCard.number}" onkeyup="changeNum();"  <c:if test="${status eq 1 or status eq 3 or sessionScope.role eq 'Dealer'}"> readonly </c:if> style="background-color: transparent"/></td>
                                <td><input type="text" name="txtAmount${integer}" id="txtAmount${integer}" value="${objShopCard.value}" readonly style="background-color: transparent"/></td>
                            </tr>
                            <c:set var="integer" value="${integer+1}"/>
                        </c:forEach>
                        <input type="hidden" id="txtStatus" name="txtStatus" value="${status}" />
                        <input type="hidden" id="txtNo" name="txtNo" value="${integer-1}"/>
                        <tr style="background-color: teal; font-style: oblique">
                            <td></td>
                            <td colspan="4" style="background-color: inherit">Amount</td>
                            <td><input type="text" id="totalAmount1" name="totalAmount1" value="" readonly style="background-color: transparent" /> </td>
                        </tr>
                        <tr style="background-color: teal; font-style: oblique">
                            <td></td>
                            <td colspan="4" style="background-color: inherit">Discount</td>
                            <td><input type="text" id="discount" name="discount" 
                                       value="${objShopCard.discount}"
                                       readonly style="background-color: transparent" /> % </td>
                        </tr>
                        <tr style="background-color: teal; font-style: oblique">
                            <td></td>
                            <td colspan="4" style="background-color: inherit">Total Amount</td>
                            <td><input type="text" id="totalAmount" name="totalAmount" value="" readonly style="background-color: transparent"/> </td>
                        </tr>
                        </c:if>
                    </table>
                    <input type="hidden" name="action" id="action" value="" />
                    <c:if test="${(status eq 0 or status eq 2) and (sessionScope.role eq 'Customer' or sessionScope.role eq 'Potential Customer')}">
                        <table cellpadding="0" cellspacing="0" border="0" style="background: transparent" align="center">
                            <tr>
                                <th colspan="5"><br/>
                                    <a href="javascript: checkoutForm()" name="checkOut" id="checkOut" class="navCart">Update</a>
                                    <a href="javascript: confirmDeleteRow()" name="remove" id="remove" class="navDel">Remove product</a>
                                    <br/>
                                </th>
                            </tr>
                        </table>
                    </c:if>
                    <input type="hidden" id="txtOrderID" name="txtOrderID" value="${param.orderID}" />
                </form>
                <form action="OrderController" method="post" id="checkReview" name="checkReview">
                    <c:if test="${sessionScope.role eq 'Dealer'}">
                    <table cellpadding="3" cellspacing="3" border="0" style="background: transparent" align="center">
                        <tr>
                            <th colspan="5"><br/>
                                <input type="hidden" id="orderID" name="orderID" value="<%= request.getParameter("orderID") %>" />
                                <input type="hidden" name="action1" id="action1" value="" />
                                <input type="hidden" name="reason" id="reason" value="" />
                                <input type="hidden" name="review" id="review" value="" />
                                <c:if test="${status eq 0 or status eq 2}">
                                    <a href="javascript: confirmReview('A')"> <img src="images/approve.png" alt="Approve"/> </a>
                                </c:if>
                                <c:if test="${status eq 0}">
                                 <a href="javascript: confirmReview('R')"> <img src="images/reject.png" alt="Reject"/> </a>
                                </c:if>
                                <br/>
                            </th>
                        </tr>
                    </table>
                    </c:if>
                </form>
                </div>
		<!-- E Body Content -->

                <!-- S Footer -->
			<jsp:include page="block/footer.jsp" />
		<!-- E Footer -->
	</div>
    </body>
</html>
